package com.clusterswisdom.horn.api.tmall.dataSynService.common;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

 
/**
 * 同步数据模型注册工厂，
 * 需要在配置文件中配置对应的表名和domain类名
 * @author li_qingsong
 *
 */
public class DBSynDomainFactory {

	private static final Logger logger = Logger.getLogger(DBSynDomainFactory.class);
	/**
	 * 需要同步的表模型对象
	 */
	private static Map<String,Object> DBSynDomains = new HashMap<String,Object>();
	
	/**
	 * 同步数据表对应domain的全限类名　其中Map的key为同步数据的表名，value 为对应的domain的全限类名
	 */
	private static Map<String,String> domainClasses = new HashMap<String,String>();
	/**
	 * 
	 */
	private static Map<String,String> classes = new HashMap<String,String>();
	
	static
	{
		domainClasses.put("T_BASE_CITY",  "com.clusterswisdom.horn.api.tmall.dataSynService.bean.TBaseCity");
		domainClasses.put("T_BASE_CITY_SAFE_STOCK",  "com.clusterswisdom.horn.api.tmall.dataSynService.bean.TBaseCitySafeStock");
		domainClasses.put("T_BASE_DEAL_INFO",  "com.clusterswisdom.horn.api.tmall.dataSynService.bean.TBaseDealInfo");
		domainClasses.put("T_BASE_DEAL_STOCK",  "com.clusterswisdom.horn.api.tmall.dataSynService.bean.TBaseDealStock");
		domainClasses.put("T_BASE_DEAL_WHOLE",  "com.clusterswisdom.horn.api.tmall.dataSynService.bean.TBaseDealWhole");
		domainClasses.put("T_BASE_FITMENT",  "com.clusterswisdom.horn.api.tmall.dataSynService.bean.TBaseFitment");
		domainClasses.put("T_BASE_HOT_CITY",  "com.clusterswisdom.horn.api.tmall.dataSynService.bean.TBaseHotCity");
		domainClasses.put("T_BASE_MATCH_TIRE",  "com.clusterswisdom.horn.api.tmall.dataSynService.bean.TBaseMatchTire");
		domainClasses.put("T_BASE_PATTERN_PIC",  "com.clusterswisdom.horn.api.tmall.dataSynService.bean.TBasePatternPic");
		domainClasses.put("T_BASE_PROV",  "com.clusterswisdom.horn.api.tmall.dataSynService.bean.TBaseProv");
		domainClasses.put("T_BASE_RANKING",  "com.clusterswisdom.horn.api.tmall.dataSynService.bean.TBaseRanking");
		domainClasses.put("T_BASE_TIRE_CAI",  "com.clusterswisdom.horn.api.tmall.dataSynService.bean.TBaseTireCai");
		domainClasses.put("T_BASE_WHOLE_SALE",  "com.clusterswisdom.horn.api.tmall.dataSynService.bean.TBaseWholeSale");
		domainClasses.put("T_BASE_WHOLE_SALE_STOCK",  "com.clusterswisdom.horn.api.tmall.dataSynService.bean.TBaseWholeSaleStock");
		try
		{
		if(domainClasses.size() > 0)
		{
			for(String tableName : domainClasses.keySet())
			{
				String domainCalssName = domainClasses.get(tableName);
				if(null !=domainCalssName && !"".equals(domainCalssName.trim()))
				{ 	
					Class<?> domainClass = Class.forName(domainCalssName);
					Object domainBean = domainClass.newInstance();
					classes.put(tableName.toUpperCase(), domainCalssName);
					DBSynDomains.put(tableName.toUpperCase(), domainBean);
				}
			}	
		}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	
	 
	public void init() throws Exception{
		if(domainClasses.size() > 0)
		{
			for(String tableName : domainClasses.keySet())
			{
				String domainCalssName = domainClasses.get(tableName);
				if(null !=domainCalssName && !"".equals(domainCalssName.trim()))
				{ 	
					Class<?> domainClass = Class.forName(domainCalssName);
					Object domainBean = domainClass.newInstance();
					classes.put(tableName.toUpperCase(), domainCalssName);
					DBSynDomains.put(tableName.toUpperCase(), domainBean);
				}
			}	
		}
		
	}
	

	public void setDomainClasses(Map<String, String> domainClasses) {
		this.domainClasses = domainClasses;
	}
     


	/**
	 * 根据表名获取数据模型对象
	 * @param tableName
	 * @return
	 */
	public static Object getDomainByTableName(String tableName){
		if(null !=tableName && !"".equals(tableName.trim()) && DBSynDomains.containsKey(tableName.toUpperCase())){
			
			return DBSynDomains.get(tableName.toUpperCase()); 
		}
		return null;
	} 
	
	/**
	 * 根据表名获取数据模型类
	 * @param tableName
	 * @return
	 */
	public static String getClassByTableName(String tableName){
		if(null !=tableName && !"".equals(tableName.trim())&& classes.containsKey(tableName.toUpperCase())){
			String domclass = classes.get(tableName.toUpperCase());
			return domclass; 
		}
		return null;
	} 
	
	/**
	 * 注册数据模型到工厂
	 * @param tableName
	 * @param domain
	 */
	public static void registeDomain(String tableName,Object domain){
		if(null !=tableName && !"".equals(tableName.trim())){
			DBSynDomains.put(tableName.toUpperCase(), domain);
		}
	}
	
	/**
	 * map转化为javabean
	 * @param type 需要转换的源bean类型
	 * @param map  需要转换的目标map对象
	 * @return
	 * @throws IntrospectionException
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 * @throws InvocationTargetException
	 */
	public static final Object toBean(Class<?> type, Map<String, ? extends Object> map)   
            throws IntrospectionException, IllegalAccessException,  InstantiationException, InvocationTargetException {  
        BeanInfo beanInfo = Introspector.getBeanInfo(type);  
        Object obj = type.newInstance();  
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();  
        for (int i = 0; i< propertyDescriptors.length; i++) {  
            PropertyDescriptor descriptor = propertyDescriptors[i];  
            String propertyName = descriptor.getName();  
            //map中的key不区分大小写全为大写，防止map中key和bean属性因头字母大小写不同而无法转换
            if (map.containsKey(propertyName.toUpperCase())) {  
                Object value = map.get(propertyName.toUpperCase());  
                Object[] args = new Object[1];  
                args[0] = value;  
                descriptor.getWriteMethod().invoke(obj, args);  
            }  
        }  
        return obj;  
    } 
	
}
